import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * $DESCRIPTION
 *
 * @author 28189
 * @since 2024/12/12 16:53
 *///TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        PageViewCounter counter =new PageViewCounter();

        Scanner sc= new Scanner(System.in);
        while (true){
            System.out.println("1、访问页面记录");
            System.out.println("2、显示访问排行");
            System.out.println("3、显示总访问量");
            System.out.println("4、显示平均访问量");
            System.out.println("5、显示最后访问时间");
            System.out.println("6、清空访问记录");
            System.out.println("7、退出");
            System.out.println("8、surprise");
            System.out.println("请输入你的选择：");
            if(sc.hasNextInt()){
            int choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1: {
                    System.out.println("请输入访问的页面url：");
                    String url = sc.nextLine();
                    counter.recordPageView(url);
                    break;
                }
                case 2: {
                    System.out.println("访问排行：");
                    counter.showTopViewedPages();
                    break;
                }
                case 3: {
                    System.out.println("总访问量：");
                    System.out.println(counter.getTotalViewCount());
                    break;
                }
                case 4: {
                    System.out.println("平均访问量：");
                    System.out.println(counter.getAverageViewCount());
                    break;
                }
                case 5: {
                    System.out.println("输入页面url");
                    String url = sc.nextLine();
                    Date lastViewTime = counter.getLastAccessTime(url);
                    if (lastViewTime == null) {
                        System.out.println("该页面没有访问记录");
                    } else {

                        System.out.println("最后访问时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EE a").format(lastViewTime) );
                    }
                    break;
                }
                case 6: {
                    counter.clearAllPageViews();
                    System.out.println("访问记录已清空");
                    break;
                }
                case 7: {
                    System.out.println("退出程序");
                    return;
                }
                //备注查询所得
                case 8: {
                    String douyin = "https://ys.mihoyo.com/cloud/";
                    if(Desktop.isDesktopSupported()&&Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)){
                        try {
                            URI uri = new URI(douyin);
                            Desktop.getDesktop().browse(uri);
                        } catch (URISyntaxException e) {
                            throw new RuntimeException(e);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
                default: {
                    System.out.println("输入错误，请重新输入");
                 }
            }
            }
        }
    }
}